#include <gtk/gtk.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

// 钢板质量计算函数
void calculate_steel_plate_weight(GtkWidget *widget, gpointer data) {
    // 获取输入控件的指针
    GtkEntry **entries = (GtkEntry **)data;
    double x = atof(gtk_entry_get_text(entries[0]));
    double y = atof(gtk_entry_get_text(entries[1]));
    double z = atof(gtk_entry_get_text(entries[2]));
    double xchamfer1 = atof(gtk_entry_get_text(entries[3]));
    double xchamfer2 = atof(gtk_entry_get_text(entries[4]));
    double ychamfer1 = atof(gtk_entry_get_text(entries[5]));
    double ychamfer2 = atof(gtk_entry_get_text(entries[6]));
    double zchamfer1 = atof(gtk_entry_get_text(entries[7]));
    double zchamfer2 = atof(gtk_entry_get_text(entries[8]));
    double quantityx1 = atof(gtk_entry_get_text(entries[9]));
    double quantityx2 = atof(gtk_entry_get_text(entries[10]));
    double quantityy1 = atof(gtk_entry_get_text(entries[11]));
    double quantityy2 = atof(gtk_entry_get_text(entries[12]));
    double quantityz1 = atof(gtk_entry_get_text(entries[13]));
    double quantityz2 = atof(gtk_entry_get_text(entries[14]));
    double density1 = atof(gtk_entry_get_text(entries[15]));

    // 计算体积
    double volume = x * y * z - (0.5 * quantityx1 * z * pow(xchamfer1, 2) +
                                 0.5 * quantityx2 * z * pow(xchamfer2, 2) +
                                 0.5 * quantityy1 * x * pow(ychamfer1, 2) +
                                 0.5 * quantityy2 * x * pow(ychamfer2, 2) +
                                 0.5 * quantityz1 * y * pow(zchamfer1, 2) +
                                 0.5 * quantityz2 * y * pow(zchamfer2, 2));

    // 计算质量
    double mass = volume * density1 * 1e-6;

    // 显示结果
    char result[50];
    snprintf(result, sizeof(result), "%.3f", mass);
    gtk_entry_set_text(entries[16], result);
}

// 柱体质量计算函数
void calculate_cylinder_weight(GtkWidget *widget, gpointer data) {
    GtkEntry **entries = (GtkEntry **)data;
    double inside_diameters = atof(gtk_entry_get_text(entries[17]));
    double outside_diameters = atof(gtk_entry_get_text(entries[18]));
    double lengths = atof(gtk_entry_get_text(entries[19]));
    double density = atof(gtk_entry_get_text(entries[15]));

    // 计算体积
    double total_volume = 0;
    total_volume += M_PI * lengths * (pow(outside_diameters, 2) - pow(inside_diameters, 2)) / 4;

    // 计算质量
    double mass = total_volume * density * 1e-6;

    // 显示结果
    char result[50];
    snprintf(result, sizeof(result), "%.3f", mass);
    gtk_entry_set_text(entries[16], result);
}

int main(int argc, char *argv[]) {
    gtk_init(&argc, &argv);

    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "质量计算器");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    GtkWidget *grid = gtk_grid_new();
    gtk_container_add(GTK_CONTAINER(window), grid);

    // 创建输入控件和标签
    GtkWidget *entries[20];
    GtkWidget *labels[20];

    const char *labels_text[] = {
        "长度X (mm)", "宽度Y (mm)", "高度Z (mm)", "X倒角1 (mm)",
        "X倒角2 (mm)", "Y倒角1 (mm)", "Y倒角2 (mm)", "Z倒角1 (mm)",
        "Z倒角2 (mm)", "数量X1", "数量X2", "数量Y1",
        "数量Y2", "数量Z1", "数量Z2", "密度 (kg/m³)",
        "结果 (kg)", "内径 (mm)", "外径 (mm)", "长度 (mm)"
    };

    // 创建输入控件和标签
    for (int i = 0; i < 20; i++) {
        labels[i] = gtk_label_new(labels_text[i]);
        entries[i] = gtk_entry_new();
        gtk_grid_attach(GTK_GRID(grid), labels[i], i % 4, i / 4 * 2, 1, 1);  // 将标签放在每个输入框的上方
        gtk_grid_attach(GTK_GRID(grid), entries[i], i % 4, i / 4 * 2 + 1, 1, 1);  // 输入框放在标签下方
    }

    // 设置密度输入栏的默认值
    gtk_entry_set_text(GTK_ENTRY(entries[15]), "7.85");

    // 创建按钮
    GtkWidget *button1 = gtk_button_new_with_label("计算钢板质量");
    GtkWidget *button2 = gtk_button_new_with_label("计算柱体质量");
    gtk_grid_attach(GTK_GRID(grid), button1, 0, 10, 4, 1); // 按钮放在最后一行
    gtk_grid_attach(GTK_GRID(grid), button2, 0, 11, 4, 1); // 按钮放在最后一行

    // 连接信号
    g_signal_connect(button1, "clicked", G_CALLBACK(calculate_steel_plate_weight), entries);
    g_signal_connect(button2, "clicked", G_CALLBACK(calculate_cylinder_weight), entries);

    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}